package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    RoomFacilityServiceImpl roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        //新增info信息
        super.saveOrUpdate(roomSubmitVo);
        if (id != null) {
            //是更新
            //删除关联表数据
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoMapper.delete(graphInfoLambdaQueryWrapper);
            //删除配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
            roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);
            //删除标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
            roomLabelMapper.delete(roomLabelLambdaQueryWrapper);
            //删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
            roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);
            //删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
            roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
            //删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
            roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);
        }
        id = roomSubmitVo.getId();

        //新增配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> facilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility facility = RoomFacility.builder()
                        .facilityId(facilityInfoId)
                        .roomId(id)
                        .build();
                facilities.add(facility);
            }
            roomFacilityService.saveBatch(facilities);
        }
        //新增标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> labels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId)
                        .roomId(id)
                        .build();
                labels.add(roomLabel);
            }
            roomLabelService.saveBatch(labels);
        }
        //新增可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm term = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId)
                        .roomId(id)
                        .build();
                roomLeaseTerms.add(term);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
        //新增支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType type = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(id)
                        .build();
                roomPaymentTypes.add(type);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //新增房间图片列表
        List<GraphVo> graphVos = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVos)) {
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVos) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //新增属性信息列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue value = RoomAttrValue.builder()
                        .attrValueId(attrValueId)
                        .roomId(id)
                        .build();
                roomAttrValues.add(value);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {

        return roomInfoMapper.pageItem(iPage, queryVo);
    }

    @Override
    public RoomDetailVo selectRoomAllInfoById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //查询公寓信息
        ApartmentInfo apartmentInfo = roomInfoMapper.selectApartmentByRoomId(id);
        //查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphInfoByItemId(ItemType.ROOM, id);
        //查询属性值信息
        List<AttrValueVo> attrValueVoList=roomAttrValueMapper.selectAttrValueByRoomId(id);
        //查询配套信息
        List<FacilityInfo> facilityInfoList=roomFacilityMapper.selectFacilityByRoomId(id);
        //查询标签信息
        List<LabelInfo> labelInfoList=roomLabelMapper.selectLabelByRoomId(id);
        //查询支付方式信息
        List<PaymentType> paymentTypeList=roomPaymentTypeMapper.selectPaymentTypeByRoomId(id);
        //查询租期信息
        List<LeaseTerm> leaseTermList=roomLeaseTermMapper.selectLeaseTearmByRoomId(id);
        //装配
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void deleteById(Long id) {
        //删除房间info
        roomInfoMapper.deleteById(id);
        //删除图片信息
        LambdaQueryWrapper<GraphInfo>graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);
        //删除属性信息
        LambdaQueryWrapper<RoomAttrValue>attrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueMapper.delete(attrValueLambdaQueryWrapper);
        //删除配套信息
        LambdaQueryWrapper<RoomFacility>roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityMapper.delete(roomFacilityLambdaQueryWrapper);
        //删除标签
        LambdaQueryWrapper<RoomLabel>roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelMapper.delete(roomLabelLambdaQueryWrapper);
        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType>roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
        //删除租期
        LambdaQueryWrapper<RoomLeaseTerm>roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);
    }
}




